package kyuu.acshmily.net
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import kyuu.acshmily.events.LoaderEvent;

	[Event(name="loadComplete", type="kyuu.acshmily.events.LoaderEvent")]

	/**
	 * 弹幕发送器
	 */
	public class CommentPoster extends EventDispatcher
	{
		private static const posterURL:String = "./post_comment.php";
		private var sender:URLLoader = null;

		public function CommentPoster(){
			sender = new URLLoader();
			sender.dataFormat = URLLoaderDataFormat.TEXT;
			sender.addEventListener(Event.COMPLETE, completeHandler);
			sender.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		}

		/**
		 * 发送弹幕信息
		 */
		public function send(vid:String, info:XML):void{
			var params:URLVariables = new URLVariables();
			params.video_id = vid;
			params.play_time = info.playTime;
			params.action = info.@mode;
			params.show_effect = info.@showEffect;
			params.hide_effect = info.@hideEffect;
			params.font_effect = -1;
			params.font_size = info.@fontSize;
			params.color = info.@color;
			params.message = info.message;

			var request:URLRequest = new URLRequest(posterURL);
			request.method = URLRequestMethod.POST;
			request.data = params;

			sender.load(request);
		}

		protected function completeHandler(e:Event):void{
			//var le:LoaderEvent = new LoaderEvent(LoaderEvent.LOAD_COMPLETE, false, false, sender.data);
			//dispatchEvent(le);
		}
		
		protected function errorHandler(e:IOErrorEvent):void{
			trace(e.text);
		}
	}
}